package edu.bmstu.rslab;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.geom.Point2D;
import java.io.IOException;

/**
 * Created by Chikh (using IntelliJ IDEA).
 * Date: 20.04.2009
 * Time: 21:00:36
 */
public class CameraStubImpl implements Camera {

    public CameraStubImpl(MobileRobot robot) {
        this.robot = robot;
    }

    public void setTranslator(Translator translator) {
        this.translator = translator;
    }

    public BufferedImage getImage() {
        BufferedImage image, diode, diodeYellow;
        try {
            image = ImageIO.read(getClass().getResource("/sample_640x480.jpg"));
            diode = ImageIO.read(getClass().getResource("/diode.bmp"));
            diodeYellow = ImageIO.read(getClass().getResource("/diode_yellow.bmp"));
        } catch (IOException e) {
            throw new RuntimeException("Fatal error: cannot read resource image", e);
        }
        Graphics2D g2 = image.createGraphics();
        Point2D robotPosition = robot.getCurrentPosition();
        if ((robotPosition != null) && (translator != null)) {
            Point2D robotImagePosition = translator.inverseTranslate(robotPosition);
            Point2D robotImageDirection = translator.inverseTranslate(robot.getCurrentDirectionPoint());
            g2.drawImage(diode, null, (int)Math.round(robotImagePosition.getX()) - (diode.getWidth()/2),
                    (int)Math.round(robotImagePosition.getY()) - (diode.getHeight()/2));
            g2.drawImage(diodeYellow, null, (int)Math.round(robotImageDirection.getX()) - (diodeYellow.getWidth()/2),
                    (int)Math.round(robotImageDirection.getY()) - (diodeYellow.getHeight()/2));
        }
        return image;
    }

    private MobileRobot robot;
    private Translator translator;
}
